package com.eco.fanliapp.admin.controller.web;


import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppOrderFeeScheme;
import com.eco.fanliapp.service.IAppOrderfeeschemeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AppOrderfeeschemeController extends BaseController {

    @Autowired
    private IAppOrderfeeschemeService appOrderfeeschemeService;

    @SysLogAnnotation(value = "分佣（杀熟）方案保存",userType = 2)
    @RequestMapping(method = RequestMethod.POST,value = "/web/order/fee/scheme/save")
    public ServerResponse save(@RequestBody AppOrderFeeScheme appOrderfeescheme){
        AppOrderFeeScheme feeScheme = new AppOrderFeeScheme();
        feeScheme.setSchemeName(appOrderfeescheme.getSchemeName());
        int size = appOrderfeeschemeService.selectAppOrderfeeschemeList(feeScheme).size();
        if(size > 1){
            return ServerResponse.createByErrorMessage("方案名称重名，请重新输入");
        }
        Integer id = appOrderfeescheme.getOrderFeeSchemeId();
        appOrderfeescheme.setUpdateTime(System.currentTimeMillis());
        if(getAuthentication().getRoleId() != 0){
            appOrderfeescheme.setCompanyId(getAuthentication().getCompanyId());
        }
        int i=0;
        if(id != null){
            i =appOrderfeeschemeService.updateAppOrderfeescheme(appOrderfeescheme);

        }else {
             appOrderfeescheme.setCreateTime(System.currentTimeMillis());
            i =appOrderfeeschemeService.insertAppOrderfeescheme(appOrderfeescheme);
        }
        if(i>0){
            return ServerResponse.createBySuccessMessage("保存成功！");
        }
        return ServerResponse.createByErrorMessage("保存失败");
    }

    @SysLogAnnotation(value = "删除分佣（杀熟）方案",userType = 2)
    @RequestMapping(method = RequestMethod.GET, value = "/web/order/fee/scheme/remove")
    public ServerResponse remove(Integer id) {

        int i = appOrderfeeschemeService.delete(id);
        if(i>0){
            return ServerResponse.createBySuccessMessage("删除成功！");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    @RequestMapping(method = RequestMethod.GET, value = "/web/order/fee/scheme/all")
    public ServerResponse<PageInfo> findGeneralfeeschemeAll(@RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Integer companyId = -1;
        if(getAuthentication().getRoleId() != 0){
            companyId = getAuthentication().getCompanyId();
        }
        AppOrderFeeScheme orderFeeScheme = new AppOrderFeeScheme();
        orderFeeScheme.setCompanyId(companyId);
        List<AppOrderFeeScheme> list = appOrderfeeschemeService.selectAppOrderfeeschemeList(orderFeeScheme);
        if (list == null){
            return ServerResponse.createByErrorMessage("查询失败");
        }
        return ServerResponse.createBySuccess("查询成功",new PageInfo(list));
    }
}
